package com.landis.temple.mobile;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TwitterStory extends ListActivity implements OnClickListener
{
    private TwitterStoryDatabase tsdb;
    private Cursor dbResults;
    private SimpleCursorAdapter adapter;
        
    // Filled with twitter user-name on credentials check
    private long myId = 0;
    private long current_story = -1;
    
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        View add_button = findViewById( R.id.create_button );
        add_button.setOnClickListener( this );
        
        tsdb = new TwitterStoryDatabase( this.getApplication().getBaseContext() );
        tsdb = tsdb.open();
        
        current_story = this.getIntent().getExtras().getLong("story_id") - 1;
        Log.d("onCreate","story_id is " + current_story );
        myId = this.getIntent().getExtras().getLong("author_id");
        
        updateList( );
    } 

    /*
     * Sets menu options for standard menu button
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.library_menu , menu);
        
        return true;
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        
        if( tsdb != null )
        {
            tsdb.close();
        }
    }

    @Override
    protected void onListItemClick( ListView list , View view , int position, long id) 
    {
        // Show details on a chapter
        // author, date created
    }
    
    private void updateList( )
    {
        if( dbResults != null )
        {
            dbResults.close();
        }
        
        dbResults = tsdb.getChapters( current_story );
        startManagingCursor(dbResults);
        
        if( dbResults != null )
        {
            // xml defined views
            int to[] = new int[] {  R.id.chapter_author   , 
                                    R.id.chapter_text     };
            
            // columns 
            String[] from = { TwitterStoryDatabase.CHAPTER_AUTHOR_ID , 
                              TwitterStoryDatabase.CHAPTER_TEXT      };
            
            adapter = new SimpleCursorAdapter( this              , 
                                               R.layout.chapters , 
                                               dbResults         , 
                                               from              , 
                                               to                );
            setListAdapter(adapter);
        }        
    }

    @Override
    public void onClick(View add_button) 
    {        
        // if library then add story else add chapter
        // Used only in OnActivityResult to determine expected results
        
        Intent i = new Intent( this , CreateActivity.class );
        
        i.putExtra( "action"    , TwitterLibrary.CREATE_CHAPTER  );
        i.putExtra( "author_id" , myId                           );
        i.putExtra( "story_id"  , current_story                  );
        
        startActivityForResult( i , TwitterLibrary.CREATE_CHAPTER );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        
        updateList();
    }
}
